package com.boho.ecp.product.service;

import java.math.BigDecimal;
import java.sql.Connection;
import java.util.ArrayList;
import java.util.List;

import org.apache.commons.lang.StringUtils;

import cn.boho.framework.po.ComboPager;
import cn.boho.framework.po.DynaBeanMap;
import cn.boho.framework.po.Pager;
import cn.boho.framework.utils.DateUtils;

import com.boho.ecp.member.dao.TProductCommentDao;
import com.boho.ecp.po.TProductPO;
import com.boho.ecp.product.dao.TProductDao;
import com.boho.ecp.util.ArrayUtils;


/**
 * 
 *
 * Copyright (c) 2012-2013 JLLT Corp. 2012-2013,All Rights Reserved.
 * This software is published under the 金麟龙腾商贸有限公司.
 * License version 1.0, a copy of which has been included with this
 * distribution in the LICENSE.txt file. 
 *
 * @File name:  ProductService.java 
 * @Create on:  2012-06-28 21:05:412
 * @Author      :  高宇舟
 *
 * @ChangeList
 * ---------------------------------------------------
 * Date         Editor              ChangeReasons
 *
 */
public class ProductService {
	
	/**
	 * 根据ID列表查询商品信息
	 * @param atx
	 * @param idList
	 * @return
	 * @throws Exception
	 * @author 陈伟
	 */
	public static List getProductByIds(Connection con, List<Integer> idList) throws Exception{
		String ids = ArrayUtils.toString(idList);
		if(StringUtils.isBlank(ids)){
			ids = "-1";
		}
		List list = TProductDao.queryProductByIds(con, ids);
		return list;
	}
	
	/**
	 * 根据ID列表查询对象列表商品信息
	 * @param atx
	 * @param idList
	 * @return
	 * @throws Exception
	 * @author 陈伟
	 */
	public static List<TProductPO> getProductPOListByIds(Connection con, List<Integer> idList) throws Exception {
		List list = getProductByIds(con, idList);
		
		List<TProductPO> poList = new ArrayList<TProductPO>();
		for (Object object : list) {
			DynaBeanMap dynaBeanMap = (DynaBeanMap)object;
			TProductPO tProductPO = new TProductPO();
			tProductPO.setProductId(dynaBeanMap.get("PRODUCT_ID")==null?null:(Integer)dynaBeanMap.get("PRODUCT_ID"));
			tProductPO.setProductNo(dynaBeanMap.get("PRODUCT_NO")==null?null:(String)dynaBeanMap.get("PRODUCT_NO"));
			tProductPO.setCategoryName(dynaBeanMap.get("CATEGORY_NAME")==null?null:(String)dynaBeanMap.get("CATEGORY_NAME"));
			tProductPO.setCategoryId(dynaBeanMap.get("CATEGORY_ID")==null?null:(Integer)dynaBeanMap.get("CATEGORY_ID"));
			tProductPO.setBrandId(dynaBeanMap.get("BRAND_ID")==null?null:(Integer)dynaBeanMap.get("BRAND_ID"));
			tProductPO.setBrandName(dynaBeanMap.get("BRAND_NAME")==null?null:(String)dynaBeanMap.get("BRAND_NAME"));
			tProductPO.setProductName(dynaBeanMap.get("PRODUCT_NAME")==null?null:(String)dynaBeanMap.get("PRODUCT_NAME"));
			tProductPO.setProductItemNo(dynaBeanMap.get("PRODUCT_ITEM_NO")==null?null:(String)dynaBeanMap.get("PRODUCT_ITEM_NO"));
			tProductPO.setProductBarcode(dynaBeanMap.get("PRODUCT_BARCODE")==null?null:(String)dynaBeanMap.get("PRODUCT_BARCODE"));
			tProductPO.setProductTag(dynaBeanMap.get("PRODUCT_TAG")==null?null:(String)dynaBeanMap.get("PRODUCT_TAG"));
			tProductPO.setProductKeyword(dynaBeanMap.get("PRODUCT_KEYWORD")==null?null:(String)dynaBeanMap.get("PRODUCT_KEYWORD"));
			tProductPO.setMarketPrice(dynaBeanMap.get("MARKET_PRICE")==null?null:(BigDecimal)dynaBeanMap.get("MARKET_PRICE"));
			tProductPO.setMemberPrice(dynaBeanMap.get("MEMBER_PRICE")==null?null:(BigDecimal)dynaBeanMap.get("MEMBER_PRICE"));
			tProductPO.setClickTimes(dynaBeanMap.get("CLICK_TIMES")==null?null:(Integer)dynaBeanMap.get("CLICK_TIMES"));
			tProductPO.setBuyTimes(dynaBeanMap.get("BUY_TIMES")==null?null:(Integer)dynaBeanMap.get("BUY_TIMES"));
			tProductPO.setMindTime(dynaBeanMap.get("MIND_TIME")==null?null:(Integer)dynaBeanMap.get("MIND_TIME"));
			tProductPO.setFavoritesTimes(dynaBeanMap.get("FAVORITES_TIMES")==null?null:(Integer)dynaBeanMap.get("FAVORITES_TIMES"));
			tProductPO.setRetunsTimes(dynaBeanMap.get("RETUNS_TIMES")==null?null:(Integer)dynaBeanMap.get("RETUNS_TIMES"));
			tProductPO.setProductStatus(dynaBeanMap.get("PRODUCT_STATUS")==null?null:(String)dynaBeanMap.get("PRODUCT_STATUS"));
			tProductPO.setPhotoUrl(dynaBeanMap.get("PHOTO_URL")==null?null:(String)dynaBeanMap.get("PHOTO_URL"));
			tProductPO.setRemarks(dynaBeanMap.get("REMARKS")==null?null:(String)dynaBeanMap.get("REMARKS"));
			tProductPO.setIsValid(dynaBeanMap.get("IS_VALID")==null?null:(Integer)dynaBeanMap.get("IS_VALID"));
			tProductPO.setCreateBy(dynaBeanMap.get("CREATE_BY")==null?null:(String)dynaBeanMap.get("CREATE_BY"));
			tProductPO.setCreateDate(DateUtils.TimestampToDate(dynaBeanMap.get("CREATE_DATE")));
			tProductPO.setUpdateBy(dynaBeanMap.get("UPDATE_BY")==null?null:(String)dynaBeanMap.get("UPDATE_BY"));
			tProductPO.setUpdateDate(DateUtils.TimestampToDate(dynaBeanMap.get("UPDATE_DATE")));
			poList.add(tProductPO);
		}
		return poList;
	}
	
	/**
	 * 根据商品编号查询评论
	 * @param con
	 * @param pager
	 * @param productId
	 * @return
	 * @throws Exception
	 */
	public static ComboPager queryTProductCommentPagerByProductId(Connection con,Pager pager, Integer productId) throws Exception {
		ComboPager cp = TProductCommentDao.queryTProductCommentPagerByProductId(con, pager, productId);
		return cp;
	}
	
	/**
	 * 根据会员编号查询评论
	 * @param con
	 * @param pager
	 * @param memberId
	 * @return
	 * @throws Exception
	 */
	public static ComboPager queryTProductCommentPagerByMemberId(Connection con,Pager pager, Integer memberId) throws Exception {
		ComboPager cp = TProductCommentDao.queryTProductCommentPagerByMemberId(con, pager, memberId);
		List list = cp.getRs();
		List idList = new ArrayList();
		for (Object object : list) {
			DynaBeanMap dbm = (DynaBeanMap)object;
			Integer productId = (Integer)dbm.get("PRODUCT_ID");
			idList.add(productId);
		}
		List productList = getProductByIds(con, idList);
		DynaBeanMap map = new DynaBeanMap();
		for (Object object : productList) {
			DynaBeanMap dbm = (DynaBeanMap)object;
			Integer productId = (Integer)dbm.get("PRODUCT_ID");
			map.put(productId, dbm);
		}
		for (Object object : list) {
			DynaBeanMap dbm = (DynaBeanMap)object;
			Integer productId = (Integer)dbm.get("PRODUCT_ID");
			DynaBeanMap m = (DynaBeanMap)map.get(productId);
			dbm.put("PRODUCT_NO", m.get("PRODUCT_NO"));
			dbm.put("PRODUCT_NAME", m.get("PRODUCT_NAME"));
		}
		return cp;
	}
	
	/**
	 * 统计评论情况
	 * @param con
	 * @param productId
	 * @return
	 * @throws Exception
	 */
	public static DynaBeanMap getTProductCommentCount(Connection con, Integer productId) throws Exception {
		DynaBeanMap dbm = new DynaBeanMap();
		dbm.put("STAR_1", 0);
		dbm.put("STAR_2", 0);
		dbm.put("STAR_3", 0);
		dbm.put("STAR_4", 0);
		dbm.put("STAR_5", 0);
		
		long count = 0;
		List list = TProductCommentDao.getTProductCommentCount(con, productId);
		for (Object object : list) {
			DynaBeanMap d = (DynaBeanMap)object;
			String score = (String)d.get("SCORE");
			Long num = (Long)d.get("NUM");
			count += num;
			dbm.put("STAR_" + score, num);
		}
		dbm.put("COUNT", count);
		
		return dbm;
	}
	
	/**
	 * 根据商品编号更新点击次数
	 * @param con
	 * @param productId
	 * @throws Exception
	 */
	public static void updateClickByProductId(Connection con, Integer productId) throws Exception {
		TProductDao.updateClickByProductId(con, productId);
	}
	
	/**
	 * 根据商品编号更新点击次数
	 * @param con
	 * @param productId
	 * @throws Exception
	 */
	public static void updateBuyByProductId(Connection con, Integer productId) throws Exception {
		TProductDao.updateBuyByProductId(con, productId);
	}
	
	/**
	 * 批量更新购买次数
	 * @param con
	 * @param idList
	 * @throws Exception
	 */
	public static void batchUpdateBuyByProductIds(Connection con, List idList) throws Exception{
		String ids = ArrayUtils.toString(idList);
		if(StringUtils.isBlank(ids)){
			ids = "-1";
		}
		TProductDao.batchUpdateBuyByProductIds(con, ids);
	}
	
	/**
	 * 根据商品编号更新意向次数
	 * @param con
	 * @param productId
	 * @return
	 * @throws Exception
	 */
	public static void updateMindByProductId(Connection con, Integer productId) throws Exception {
		TProductDao.updateMindByProductId(con, productId);
	}
	
	/**
	 * 根据商品编号更新收藏次数
	 * @param con
	 * @param productId
	 * @return
	 * @throws Exception
	 */
	public static void updateFavByProductId(Connection con, Integer productId) throws Exception {
		TProductDao.updateFavByProductId(con, productId);
	}
	
	/**
	 * 根据商品编号更新反馈次数
	 * @param con
	 * @param productId
	 * @return
	 * @throws Exception
	 */
	public static void updateReturnByProductId(Connection con, Integer productId) throws Exception {
		TProductDao.updateReturnByProductId(con, productId);
	}
	
	/**
	 * 批量更新反馈数
	 * @param con
	 * @param productIds
	 * @throws Exception
	 */
	public static void batchUpdateReturnByProductIds(Connection con, List productIds) throws Exception {
		String ids = ArrayUtils.toString(productIds);
		if(StringUtils.isBlank(ids)){
			ids = "-1";
		}
		TProductDao.batchUpdateReturnByProductIds(con, ids);
	}
	
	public static void main(String[] args) {
		DynaBeanMap dbm = new DynaBeanMap();
	}
}
